package LinkedList;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class Test {
    public static void main1(String[] args) {
        List<Integer> list = new LinkedList<>();
        LinkedList<String> list2 = new LinkedList<>();
        list2.add("JavaSE");
        list2.add("JavaWeb");
        list2.add("JavaEE");

        List<String> list3 = new LinkedList<>(list2);
        System.out.println(list3);//集合类型的插入
        LinkedList<Integer> linkedList = new LinkedList<>();

        linkedList.add(1);//默认都是尾插
        linkedList.addFirst(2);
        linkedList.addLast(3);
        linkedList.addLast(5);
        System.out.println(linkedList);

        LinkedList list4 = new LinkedList();
        list4.add(11);
        list4.add(21);
        list4.add(31);

        linkedList.addAll(list4);
        System.out.println(linkedList);


        linkedList.remove(2);//会看到我们删除的是指定的是下标，不是元素
        System.out.println(linkedList);

        //删除遇到的集合
        linkedList.removeAll(list4);
        System.out.println(linkedList);

        //获取下标 index 位置元素
        System.out.println(linkedList.get(1));

        //将下标 index 位置元素设置为 element
        linkedList.set(0,5);
        System.out.println(linkedList);

        //判断 o 是否在线性表中
        System.out.println(linkedList.contains(5));

        //返回第一个元素 o 所在下标
        System.out.println(linkedList.indexOf(5));

        //返回最后一个 o 的下标
        System.out.println(linkedList.lastIndexOf(5));

        //截取部分list,区间是左闭右开
        System.out.println(linkedList.subList(1, 3));

        linkedList.clear();
        System.out.println(linkedList);
    }



    //重点：linkedlist遍历
    public static void main(String[] args) {
        LinkedList<Integer> linkedList = new LinkedList<>();
        linkedList.add(1);//默认都是尾插
        linkedList.addFirst(2);
        linkedList.addLast(3);
        linkedList.addLast(5);
        linkedList.addLast(11);
        linkedList.addLast(21);
        linkedList.addLast(31);


        /**
         * 迭代器：自动遍历，判断下一元素存在，存在就打印并往后移动一步
         * listIterator是Iterator的子类，它是专门用于打印list类型的迭代器，即是线性表
         */
        System.out.println("========   iterator   ========");
        Iterator<Integer> iterator = linkedList.iterator();
        while(iterator.hasNext()){
            System.out.print(iterator.next() + " ");
        }
        System.out.println();


        System.out.println("========   ListIterator正向遍历   ========");
        //正向遍历
        ListIterator<Integer> listIterator = linkedList.listIterator();
        while(listIterator.hasNext()){
            System.out.print(listIterator.next() + " ");
        }
        System.out.println();


        System.out.println("========   ListIterator反向遍历   ========");
        //反向遍历：反向索引，若前一个元素存在，则向前移动并打印
        ListIterator<Integer> listIterator1 = linkedList.listIterator(linkedList.size());
        while(listIterator1.hasPrevious()){
            System.out.print(listIterator1.previous() + " ");
        }
        System.out.println();



        System.out.println("========for each========");
        for (Integer x:
             linkedList) {
            System.out.print(x + " ");
        }

        System.out.println();
        System.out.println("======== for ========");
        for (int i = 0; i < linkedList.size(); i++) {
            System.out.print(linkedList.get(i) + " ");
        }

    }
}
